const gamewidth=gameDom.clientWidth;
class Pipe extends Rectangle{//单个水管
    constructor(height,top,speed,dom){
        super(52,height,gamewidth,top,speed,0,dom)
    }

    onMove(){
        if(this.left<-this.width){
            this.dom.remove();//当管道移除屏幕后，remove掉

        }
    }
}


function getRandom(min,max){
    return Math.floor(Math.random()*(max-min)+min);
}
class PipePare{//水管对，单独的类

    constructor(speed){
        this.spaceHeight=150;//水管间隙高度
        this.minHeight=80;//水管最短高度
        this.maxHeight=landTop-this.minHeight-this.spaceHeight;
        const upHeight=getRandom(this.minHeight,this.maxHeight);//随机生成上水管的高度
        const upDom=document.createElement('div'); 
        upDom.className="pipe up";
        this.upPipe=new Pipe(upHeight,0,speed,upDom)

        const downHeight=landTop-upHeight-this.spaceHeight;
        const downTop=landTop-downHeight;

        const downDom=document.createElement("div");
        downDom.className='pipe down';
        this.downPipe=new Pipe(downHeight,downTop,speed,downDom)
    
        gameDom.appendChild(upDom);
        gameDom.appendChild(downDom);

    }
    
    get useLess(){
        return this.upPipe.left<-this.upPipe.width;
    }

    move(duration){
        this.upPipe.move(duration);
        this.downPipe.move(duration);
    }
}

class PipePareProducer{//用于产生水管对
    constructor(speed){
        this.speed=speed;
        this.pares=[];
        this.timer=null;
        this.tick=2000;
    }
    startProducer(){
        if(this.timer){return;}
        this.timer=setInterval(()=>{
            this.pares.push(new PipePare(this.speed));
            //移掉没用的水管
            for(let i=0;i<this.pares.length;i++){
                var pare=this.pares[i];
                if(pare.useLess){
                    this.pares.splice(i,1);
                    i--;
                }
            }
        },this.tick)
    }

    stopProducer(){
        clearInterval(this.timer);
        this.timer=null;
    }
}

// var pare=new PipePareProducer(-100);
// pare.startProducer();
// setInterval(() => {
//     pare.pares.forEach(pare=>{
//         pare.move(16/1000);
//     })
// }, 16);